<?php

class AuthController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('layout-login');
    }

    public function indexAction()
    {
        $loginForm = new Application_Form_Auth();
        $loginForm->submit->setLabel('login');
        $this->view->form = $loginForm;
    }

//Funcoin de inicio de session
    public function loginAction()
    {
        // action body
        $loginForm = new Application_Form_Auth();
        $loginForm->submit->setLabel('login');
        $this->view->form = $loginForm;
 
        if ($loginForm->isValid($_POST)) {
            $username = $loginForm->getValue('username');
            $password = $loginForm->getValue('password');
           try{
                //Instancia de la tabla de usuarios
                $auth   = new Application_Model_DbTable_User();
                $result = $auth->authenticate($username,$password);
     
                if ($result) {
                   $this->view->username = $username;    
                    $this->_redirect('/client');
                }
                else{
                   $this->view->error_message = self::ATHENTICATION_ERROR_MESSAGE;
                }
            }
            catch(Exception $e){
                $logger = Zend_Registry::get('logger');     
                $logger->log($e->getMessage()."; File Name: ".$e->getFile ( )."; Line: ".$e->getLine ( ), Zend_Log::ERR);
               // $this->view->error_message = self::ERROR_MESSAGE;
            }
        }

 
        $this->view->loginForm = $loginForm;

    }
    //Funcion de cierre de session
    public function logoutAction()
    {
        // action body
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();//Limpia la session 
        $this->_redirect('auth/login');
    }


}



